<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:15%;height: 70%;overflow-y: hidden">
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" id="MessageModal">
          <a-tab-pane key="1" style="width: 750px;float: left;" tab="學生信息" id="MessageModal" forceRender>
            <h3>學生信息</h3>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名称">
                {{model.stuName}}
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="成绩">
                <a-input placeholder="请输入成绩" v-decorator.trim="[ 'fraction']" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="班级">
                <a-input placeholder="请输入班级" v-decorator.trim="[ 'cname' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="身份证号码">
                <a-input placeholder="请输入身份证号" v-decorator.trim="[ 'stuId' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系电话">
                <a-input placeholder="请输入联系电话" v-decorator.trim="[ 'stuPhone' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="地址">
                <a-input placeholder="地址" v-decorator.trim="[ 'stuAddress' ]" />
              </a-form-item>
            </a-col>
          </a-tab-pane>

          <a-tab-pane key="2" style="width: 750px;float: left;" tab="疫苗信息" id="pcrModal" forceRender>
            <h3>疫苗信息</h3>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="第一针疫苗">
                <a-input placeholder="请选择接种疫苗第一针时间" v-decorator.trim="[ 'stuVero1' ]" type="date" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="第二针疫苗">
                <a-input placeholder="请选择接种疫苗第二针时间" v-decorator.trim="[ 'stuVero2' ]" type="date" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="第三针疫苗">
                <a-input placeholder="请选择接种疫苗第三针时间" v-decorator.trim="[ 'stuVero3' ]"  type="date" />
              </a-form-item>
            </a-col>
          </a-tab-pane>

          <a-tab-pane key="3" tab="父母信息" style="width: 750px;float: left;" id="ParentMdal" forceRender>
            <h3>父母信息</h3>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="學生名称">
                <a-input placeholder="请输入请输入名称" v-decorator.trim="[ 'stuName', validatorRules.supplier]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="家長名称">
                <a-input placeholder="请输入请输入名称" v-decorator.trim="[ 'parentName', validatorRules.supplier]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="家長电话号">
                <a-input placeholder="请输入电话号" v-decorator.trim="[ 'parentPhone' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="家長身份证号">
                <a-input placeholder="请输入身份证号" v-decorator.trim="[ 'parentId' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="家長职业">
                <a-input placeholder="请输入职业" v-decorator.trim="[ 'parentCareer' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="关系">
                <a-input placeholder="请输入关系" v-decorator.trim="[ 'parentRelation' ]" />
              </a-form-item>
            </a-col>
          </a-tab-pane>

          <a-tab-pane key="4" tab="圖片" id="materialHeadModal" forceRender>
            <a-col :lg="12" :md="12" :sm="24">
              <a-form-item :labelCol="{xs: { span: 24 },sm: { span: 4 }}" :wrapperCol="{xs: { span: 24 },sm: { span: 20 }}" label="学生图片">
                <j-image-upload-fdfs v-model="fileList" bizPath="material" text="上传" isMultiple></j-image-upload-fdfs>
              </a-form-item>
            </a-col>
          </a-tab-pane>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import {addStudent,editStudent,checkStudent } from '@/api/api'
  import {autoJumpNextInput} from "@/utils/util"
  import JImageUploadFdfs from '@/components/jeecg/JImageUploadFdfs'
  export default {
    name: "MessageModal",
    components:{
      JImageUploadFdfs
    },
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        isReadOnly: false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          student:{
            rules: [
              { required: true, message: '请输入名称!' },
              { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur' },
              { validator: this.validateSupplierName}
            ]
          }
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'stuName', 'stuClass','cname', 'stuId','fraction', 'stuPhone', 'stuVero1',
            'stuVero2', 'stuVero3', 'stuAddress','parentName','parentPhone','parentId','parentCareer','parentRelation'))
          autoJumpNextInput('messageModal')
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            if(this.model.beginNeedGet && this.model.beginNeedPay) {
              that.$message.warn("期初应收和期初应付不能同时输入");
              that.confirmLoading = false;
              return;
            }
            formData.type = "客户";
            let obj;
            if(!this.model.id){
              obj=addStudent(formData);
            }else{
              obj=editStudent(formData);
            }
            obj.then((res)=>{
              if(res.code === 200){
                that.$emit('ok');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      validateSupplierName(rule, value, callback){
        let params = {
          name: value,
          type: '客户',
          id: this.model.id?this.model.id:0
        };
        checkSupplier(params).then((res)=>{
          if(res && res.code===200) {
            if(!res.data.status){
              callback();
            } else {
              callback("名称已经存在");
            }
          } else {
            callback(res.data);
          }
        });
      }
    }
  }
</script>
<style scoped>

</style>